import Vue from "vue";
import VueRouter from "vue-router";
import Index from "@/views/index.vue";

import Notfound from "@/views/404.vue";
import Home from "@/views/Home.vue";
import UserRegister from "@/views/UserRegister.vue";
import UserLogin from "@/views/UserLogin.vue";

import InfoShow from "@/views/InfoShow.vue";
import SetMate from "@/views/SetMate.vue";
import Check from "@/views/Check.vue";
import CheckOne from "@/views/CheckOne.vue";
import Analysis from "@/views/Analysis.vue";
import ScoreSum from "@/views/ScoreSum.vue";
import Admin from "@/views/Admin.vue";
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/index",
  },
  {
    path: "/index",
    name: "index",
    component: Index,
    children: [
      {
        path: "/home",
        name: "home",
        component: Home,
      },
      {
        path: "",
        component: Home,
      },
      {
        path: "/infoshow",
        name: "infoshow",
        component: InfoShow,
      },
      {
        path: "/setmate",
        name: "setmate",
        component: SetMate,
      },
      {
        path: "/check",
        name: "check",
        component: Check,
      },
      {
        path: "/checkone",
        name: "checkone",
        component: CheckOne,
      },
      {
        path: "/analysis",
        name: "analysis",
        component: Analysis,
      },
      {
        path: "/scoresum",
        name: "scoresum",
        component: ScoreSum,
      },
      {
        path: "/admin",
        name: "admin",
        component: Admin,
      },
    ],
  },
  {
    path: "/register",
    name: "userregister",
    component: UserRegister,
  },
  {
    path: "*",
    name: "notfound",
    component: Notfound,
  },
  {
    path: "/login",
    name: "userlogin",
    component: UserLogin,
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});

//路由守卫

router.beforeEach((to, from, next) => {
  if (to.name === "userlogin" || to.name === "userregister") {
    next();
  } else {
    const token = localStorage.getItem("token");
    if (token) {
      next();
    } else {
      next("/login");
    }
  }
});
export default router;
